<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Friends extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function MyFriend(Request $request)
    {
        $data = $request->param();

//        isset($data['page']) ? $page = $data['page'] : $page = 1;
//        isset($data['listrow']) ? $rows = $data['listrow'] : $rows = 20;
        $res = \db('home_friends')
            ->alias('F')
            ->join('home_friends R','F.friendid = R.uid')
            ->join('home_users U','F.friendid=U.id')
            ->where(['F.uid'=>$data['id'],'R.friendid'=>$data['id'],'F.status'=>1,'R.status'=>1])
            ->field('U.id uid,U.nickname,U.headicon,F.friendid,U.role')
            ->select();
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(1,'无数据',$res);
        }
//        ->each(function ($item){
//        $isfr = \db('home_friends')->where(['uid'=>$item['friendid'],'status'=>1])->count();
//        if ($isfr){
//            return $item;
//        }else{
//            unset($item);
//        }
//    })
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function addFriend(Request $request)
    {
        $data = $request->param();
        if (empty($data['id']) || empty($data['toid'])){
            return apistatus(1,'缺少请求数据');
        }
        self::checktoken($data['id']);
        $role = $data['role'];
        if ($role != 1){
            return apistatus(0,'无加好友权限');
        }
        if (isfriend($data['id'],$data['toid'])){
            return apistatus(0,'你们已经是好友请开始聊天吧');
        }
        $exist = \db('home_friends')->where(['uid'=>$data['id'],'friendid'=>$data['toid']])->find();
        if ($exist['status'] == 1){
            $res = \db('home_friends')->where('id',$exist['id'])->update(['status'=>2]);
            $re['status'] = 0;
        }elseif ($exist['status'] == 2){
            $res = \db('home_friends')->where('id',$exist['id'])->update(['status'=>1]);
            $re['status'] = 1;
        }elseif(empty($exist)){
            $insert['fromid'] = $data['id'];
            $insert['toid'] = $data['toid'];
            $insert['create_time'] = time();
            $time = ['between',[mktime(0,0,0,date('m'),date('d'),date('Y')),mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1]];
            $apply = \db('home_apply')->where(['fromid'=>$data['id'],'toid'=>$data['toid'],'create_time'=>$time])->count();
            if (count($apply)> 3) return apistatus(0,'您本日添加该好友次数过于频繁,请等待对方操作');
            $res = \db('home_apply')->insert($insert);
            $re['status'] = 1;
        }
        if ($res){
            $news['fromid'] = $data['id'];
            $news['toid'] = $data['toid'];
            $news['news_content'] = '申请添加你为好友';
            $news['create_time'] = time();
            $news['type'] = 1;
            $id = \db('home_news')->insertGetId($news);
            if ($id){
                return apistatus(1,'操作成功',$re);
            }else{
                return apistatus(0,'操作失败');
            }
            return apistatus(1,'操作成功',$re);
        }else{
            return apistatus(1,'操作失败');
        }
    }

    /**
     * 同意或拒绝添加好友
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function acceptFriend(Request $request)
    {
        $data = $request->param();
        if ($data['type'] == 1){
            if (isfriend($data['id'],$data['toid'])){
                  return apistatus(0,'您已添加对方为好友请勿重复操作');
            }
            $insert['uid'] = $data['id'];
            $insert['friendid'] = $data['toid'];
            $insert['create_time'] = time();
            $result = \db('home_friends')->insert($insert);
            if ($result){
                \db('home_apply')
                    ->where(['fromid'=>$data['id'],'toid'=>$data['toid']])
                    ->update(['status'=>3]);
                $news['fromid'] = $data['id'];
                $news['toid'] = $data['toid'];
                $news['news_content'] = '同意你的好友申请';
                $news['create_time'] = time();
                $news['type'] = 1;
                \db('home_news')->insert($news);
                $insert['uid'] = $data['toid'];
                $insert['friendid'] = $data['id'];
                \db('home_friends')->insert($insert);
                return apistatus(1,'好友添加成功');
            }else{
                return apistatus(0,'申请失败请重试');
            }
        }elseif($data['type'] == 2) {
            \db('home_apply')
                ->where(['fromid'=>$data['id'],'toid'=>$data['toid']])
                ->update(['status'=>4]);
            $news['fromid'] = $data['id'];
            $news['toid'] = $data['toid'];
            $news['news_content'] = '拒绝了你的好友申请';
            $news['create_time'] = time();
            $news['type'] = 1;
            \db('home_news')->insert($news);
            return apistatus(1,'拒绝成功');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function cancleFollow(Request $request)
    {
        $data = $request->param();


    }
    //是否为好友
    public function isFriend(Request $request){
        $data = $request->param();
        self::checktoken($data['id']);
        $info = \model('User')->where('id',$data['toid'])
            ->field('headicon,nickname,role')
            ->find()
            ->toarray();
        $isfriend = isfriend($data['id'],$data['toid']);
        if ($isfriend){
           $info['isfriend'] = '是好友';
        }else{
            $info['isfriend'] = '不是好友';
        }
        if ($info['role'] == 1){
            $info['ischat'] = isChat($data['id'],$data['toid']);
        }else{
            $info['ischat'] = isConsult($data['id'],$data['toid']);
        }
        if ($info['ischat']){
            $info['contact'] = \db('home_users')->where('id',$data['toid'])->field('phonenum,wechat_num')->find();
        }else{
            $info['contact'] = '';
        }
        return apistatus(1,'操作成功',$info);
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    

}
